package bom.controller.organization;


import bom.configuration.util.JsonHelper;
import bom.pojo.organization.OrgGroup;
import bom.pojo.organization.User;
import bom.pojo.query.Page;
import bom.pojo.query.QueryUser;
import bom.service.i.organization.OrgGroupService;
import bom.service.i.organization.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: Fancyears·Maylos·Malvis
 * @Description: 组织架构的操作类
 * @Date: Created in  2018/11/27 16:03
 * @Modified By:
 */
@Controller
@RequestMapping("/group")
public class OrgGroupController {

    @Autowired
    private OrgGroupService orgGroupService;
    @Autowired
    private UserService userService;

    /**
     * 功能描述：跳转到更新用户的页面
     *
     * @param entity
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateUserPage")
    public String updateUserPage(User entity, Model model) throws Exception {
        entity = userService.get(entity);
        entity.setRoleArray(JsonHelper.list2json(Optional.ofNullable(userService.findByLogin(entity.getLogin())).filter(u -> u != null).orElse(new User()).getRoles()));
        model.addAttribute("entity", entity);
        return "";
    }

    /**
     * 跳转到添加用户的页面
     *
     * @throws Exception
     */
    @RequestMapping(value = "/addUserPage")
    public String addUserPage() throws Exception {
        return "";
    }

    @RequestMapping(value = "/update")
    public Map<String, Object> update(OrgGroup entity) throws Exception {
        return orgGroupService.updateOrg(entity);
    }

    public Map<String, Object> save(OrgGroup entity) throws Exception {
        String max_node = getMaxNode(orgGroupService.getMaxOrgGroup(entity.getOrgGroup().getNode()), entity.getOrgGroup().getNode());
        entity.setParentNode(entity.getOrgGroup().getNode());
        entity.setNode(max_node);
        return orgGroupService.save(entity);
    }

    @RequestMapping(value = "/updateGroupPage")
    //@ResponseBody
    public String updateGroupPage(OrgGroup entity, Model model) throws Exception {
        entity = orgGroupService.get(entity);
        entity.setOrgGroup(orgGroupService.findByNode(entity.getParentNode()));
        model.addAttribute("entity", entity);
        //return getPageBaseRoot()+UPDATEPAGE;
        return "sys/orggroup/update";
    }

    @RequestMapping(value = "/addGroupPage")
    //@ResponseBody
    public String addPage(OrgGroup entity, Model model) throws Exception {
        entity = orgGroupService.get(entity);
        model.addAttribute("entity", entity);
        //return getPageBaseRoot()+ADDPAGE;
        return "sys/orggroup/add";
    }

    /**
     * 功能描述：获取组织架构底下的相应的用户
     *
     * @return
     */
    @RequestMapping(value = "/userList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> userList(QueryUser user) {
        Map<String, Object> result = new HashMap<String, Object>();
        user.setSort(User.reflectToDBField(user.getSort()));
        Page page = userService.findByGroupUserPage(user);
        result.put("totalCount", page.getTotal());
        result.put("result", page.getRows());
        return result;
    }

    /**
     * 功能描述：获取组织架构的整颗菜单树
     *
     * @return
     */
    @RequestMapping(value = "/loadGroupTree", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> loadGroupTree() {
        return orgGroupService.loadGroupTree();
    }


    /**
     * 功能描述：实现批量删除数据字典的记录
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, Object> remove(OrgGroup entity) throws Exception {
        return orgGroupService.remove(entity);
    }

    private String getMaxNode(String node, String parentNode) {
        String max_node = "";
        if (node == null) {
            max_node = parentNode + "001";
        } else {
            String n = (Integer.parseInt(node.substring(node.length() - 3)) + 1) + "";
            switch (n.length()) {
                case 1:
                    max_node = parentNode + "00" + n;
                    break;
                case 2:
                    max_node = parentNode + "0" + n;
                    break;
                case 3:
                    max_node = parentNode + "" + n;
                    break;
            }
        }
        return max_node;
    }
}
